﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Calendar;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace SportManager
{
    public partial class MainForm : Form
    {
        private List<Training> Trainings = new List<Training>();
        private List<AntropometricalData> Antropometrics = new List<AntropometricalData>();

        public MainForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.ActiveControl = mainCalendar;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }  

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox dlg = new AboutBox();
            dlg.ShowDialog();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ShowExercisesListDialog();
        }

        private void ShowExercisesListDialog()
        {
            ExercisesListDlg dlg = new ExercisesListDlg();
            dlg.Show();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Program.TrainingsKeeper.SaveData();
            Program.ExercisesKeeper.SaveData();
            Program.AntropometricKeeper.SaveData();
            Program.OptionsKeeper.SaveData();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            ShowAntropometricDlg();
        }

        private void ShowAntropometricDlg(int dataIndex = -1)
        {
            AntropometricDlg dlg = (dataIndex >= 0) ? new AntropometricDlg(Antropometrics[dataIndex]) : new AntropometricDlg();

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                int itemIndex = Program.AntropometricKeeper.GetItemIndex(dlg.Result.Id);
                if (itemIndex >= 0)
                {
                    Program.AntropometricKeeper.GetData()[itemIndex] = dlg.Result;
                }
                else
                {
                    Antropometrics.Add(dlg.Result);
                    PlaceItems();
                }
            }
        }        

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            ShowStatisticDialog();
        }

        private void ShowStatisticDialog()
        {
            StatisticDlg dlg = new StatisticDlg();
            dlg.ShowDialog();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Trainings = Program.TrainingsKeeper.GetData();
            Antropometrics = Program.AntropometricKeeper.GetData();
            InitializeCalendars();
        }

        private void ShowOptionsDialog()
        {
            OptionsDlg dlg = new OptionsDlg();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
            }
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowOptionsDialog();
        }

        private void toolStripButton2_Click_1(object sender, EventArgs e)
        {
            ShowOptionsDialog();
        }        

        private void statisticToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowStatisticDialog();
        }

        private void antropometricToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowAntropometricDlg(-1);
        }

        private void exerciseListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowExercisesListDialog();
        }        

        private void toolStripButton3_Click_1(object sender, EventArgs e)
        {
            ShowToday();
        }

        protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);
            if (mainCalendar.Focused)
            {
                monthView1.ViewStart = mainCalendar.ViewStart.Subtract(new TimeSpan(GetDaysInPreviousMonth(), 0, 0, 0));
            }
            else
            {
                DateTime firstMonthDay = monthView1.ViewStart.AddMonths(1);
                DateTime lastMonthDay = monthView1.ViewStart.AddMonths(2);

                mainCalendar.SetViewRange(firstMonthDay, lastMonthDay);
            }
        }

        private int GetDaysInPreviousMonth()
        {
            int dayInPreviousMonth = 0;
            if (mainCalendar.ViewStart.Month == 1)
            {
                dayInPreviousMonth = DateTime.DaysInMonth(mainCalendar.ViewStart.Year - 1, 12);
            }
            else
            {
                dayInPreviousMonth = DateTime.DaysInMonth(mainCalendar.ViewStart.Year, (mainCalendar.ViewStart.Month - 1));
            }

            return dayInPreviousMonth;
        }

        private void ShowToday()
        {
            DateTime today = DateTime.Today;
            switch (CurrentCalendarMode)
            {
                case CalendarMode.MONTH_MODE:
                    {
                        DateTime firstMonthDay = new DateTime(today.Year, today.Month, 1);
                        DateTime lastMonthDay = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
                        mainCalendar.SetViewRange(firstMonthDay, lastMonthDay);
                        monthView1.ViewStart = mainCalendar.ViewStart.Subtract(new TimeSpan(GetDaysInPreviousMonth(), 0, 0, 0));
                        break;
                    }
                case CalendarMode.WEEK_MODE:
                    {
                        DateTime firstWeekDay = today.Subtract(new TimeSpan(((int)today.DayOfWeek - 1), 0, 0, 0));
                        DateTime lastWeekDay = firstWeekDay.AddDays(DaysInWeek - 1);
                        mainCalendar.SetViewRange(firstWeekDay, lastWeekDay);
                        monthView1.ViewStart = mainCalendar.ViewStart.Subtract(new TimeSpan(GetDaysInPreviousMonth(), 0, 0, 0));
                        break;
                    }
                case CalendarMode.DAY_MODE:
                    {
                        mainCalendar.SetViewRange(today, today);
                        monthView1.ViewStart = mainCalendar.ViewStart.Subtract(new TimeSpan(GetDaysInPreviousMonth(), 0, 0, 0));
                        break;
                    }
                default:
                    throw new Exception("Wrong calendar mode type!");
            }

            ShowDateInToolbox();
        }

        private void ShowDateInToolbox()
        {
            StringBuilder dateStart = new StringBuilder();
            StringBuilder dateFinish = new StringBuilder();
            System.Globalization.DateTimeFormatInfo myInvariantInfo = new System.Globalization.DateTimeFormatInfo();

            if (CurrentCalendarMode == CalendarMode.MONTH_MODE)
            {
                dateStart.Append(myInvariantInfo.GetMonthName(mainCalendar.ViewStart.Month));
                if (mainCalendar.ViewStart.Year == mainCalendar.ViewEnd.Year)
                {
                    if (mainCalendar.ViewStart.Month != mainCalendar.ViewEnd.Month)
                    {
                        dateFinish.Append(" - " + myInvariantInfo.GetMonthName(mainCalendar.ViewEnd.Month));
                    }
                }
                else
                {
                    dateStart.Append("  " + mainCalendar.ViewStart.Year + " - ");
                    dateFinish.Append(myInvariantInfo.GetMonthName(mainCalendar.ViewEnd.Month));
                }

                dateFinish.Append("  " + mainCalendar.ViewEnd.Year);
            }

            if ((CurrentCalendarMode == CalendarMode.WEEK_MODE) || (CurrentCalendarMode == CalendarMode.DAY_MODE))
            {
                if (mainCalendar.ViewStart.Date == mainCalendar.ViewEnd.Date)
                {
                    dateFinish.Append(mainCalendar.ViewEnd.Day.ToString());
                    dateFinish.Append("  " + myInvariantInfo.GetMonthName(mainCalendar.ViewEnd.Month));
                    dateFinish.Append("  " + mainCalendar.ViewEnd.Year);
                }
                else
                {
                    dateStart.Append(mainCalendar.ViewStart.Day.ToString());
                    if (mainCalendar.ViewStart.Year == mainCalendar.ViewEnd.Year)
                    {
                        if (mainCalendar.ViewStart.Month != mainCalendar.ViewEnd.Month)
                        {
                            dateStart.Append("  " + myInvariantInfo.GetMonthName(mainCalendar.ViewStart.Month));
                        }
                    }
                    else
                    {
                        dateStart.Append("  " + myInvariantInfo.GetMonthName(mainCalendar.ViewStart.Month));
                        dateStart.Append("  " + mainCalendar.ViewStart.Year + " - ");
                    }
                    dateFinish.Append(" - " + mainCalendar.ViewEnd.Day.ToString());
                    dateFinish.Append("  " + myInvariantInfo.GetMonthName(mainCalendar.ViewEnd.Month));
                    dateFinish.Append("  " + mainCalendar.ViewEnd.Year);
                }
            }

            toolStripLabel3.Text = dateStart.ToString() + dateFinish.ToString();
        }

        private void AddTrainingSeriesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TrainingSeriesWizard.AddTrainingSeriesWizard form = new TrainingSeriesWizard.AddTrainingSeriesWizard();
            form.ShowDialog();
        }
    }
}